package com.zhangxin.designmode.designpattern.prototype.v1;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author zhangxin
 * @date 2022/5/6
 */
public class Demo {

    private ConcurrentHashMap<String, SearchWord> concurrentKeywords = new ConcurrentHashMap<>();
    private long lastUpdateTime = -1;

    public void refresh() {
        // 从数据库中取出更新时间 > lastUpdateTime 的数据，放入到 concurrentKeywords 中
        List<SearchWord> toBeUpdateSearchWords = getSearchWords(lastUpdateTime);
        long maxNewUpdatedTime = lastUpdateTime;
        assert toBeUpdateSearchWords != null;
        for (SearchWord searchWord : toBeUpdateSearchWords) {
            if (searchWord.getLastUpdateTime() > maxNewUpdatedTime) {
                maxNewUpdatedTime = searchWord.getLastUpdateTime();
            }

            if (concurrentKeywords.containsKey(searchWord.getKeyword())) {
                concurrentKeywords.replace(searchWord.getKeyword(), searchWord);
            } else {
                concurrentKeywords.put(searchWord.getKeyword(), searchWord);
            }
        }

        lastUpdateTime = maxNewUpdatedTime;

    }

    private List<SearchWord> getSearchWords(long lastUpdateTime) {
        // TODO: 从数据库中取出更新时间>lastUpdateTime的数据
        return null;
    }
}
